<template>
    <div class="bixbox">
      <div class="login-box">
        <div class="title-container">
          <div class="title"><b>欢迎</b>注册</div>
          <div class="subtitle">智能社交平台</div>
        </div>
        <form @submit.prevent="register">
          <div class="input-group">
            <input type="text" id="username" v-model.trim="user.username" required placeholder=" ">
            <label for="username">用户名</label>
          </div>
          <div class="input-group">
            <input type="text" id="phone" v-model.trim="user.phone" required placeholder=" " pattern="[0-9]{11}" title="请输入11位手机号码">
            <label for="phone">手机号码</label>
          </div>
          <div class="input-group">
            <input type="password" id="password" v-model.trim="user.password" required placeholder=" ">
            <label for="password">密码</label>
          </div>
          <div class="input-group">
            <input type="password" id="confirmPassword" v-model.trim="user.confirmPassword" required placeholder=" " @blur="checkPasswordMatch">
            <label for="confirmPassword">确认密码</label>
          </div>
          <button type="submit" class="l1">注册</button>
        </form>
        <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
        <button class="login-button" @click="goToLogin">已有账号？去登录</button>
      </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        user: {
          username: '',
          phone: '',
          password: '',
          confirmPassword: ''
        },
        errorMessage: ''
      };
    },
    methods: {
      async register() {
        if (this.user.password!== this.user.confirmPassword) {
          this.errorMessage = "两次输入的密码不一致";
          return;
        }
        try {
          const response = await axios.post('http://localhost:8089/register', this.user);
          if (response.data.code === 100) {
            alert("注册成功");
            window.location.replace("login.html");
          } else {
            alert("注册失败！");
            this.errorMessage = response.data.message;
          }
        } catch (error) {
          console.error('Register error:', error);
          this.errorMessage = "注册失败！请检查网络或稍后再试";
        }
      },
      checkPasswordMatch() {
        if (this.user.password!== this.user.confirmPassword) {
          this.errorMessage = "两次输入的密码不一致";
        } else {
          this.errorMessage = "";
        }
      },
      goToLogin() {
        this.$router.push('/login');
        alert('跳转到登录页面'); 
      }
    }
  };
  </script>
  
  <style scoped>
  body {
    
    width: 100%;
    height: 100vh;
    background: linear-gradient(135deg, #1e1e1e, #000);
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    color: #fff;
  }
  
  .bixbox {
    width: 100%;
    height: 100vh;
    background-color: #9ed374;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    box-sizing: border-box;
  }
  
  .login-box {
    max-width: 450px;
    width: 100%;
    padding: 40px;
    background-color: rgb(255, 255, 255);
    border-radius: 15px;
    box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.18);
    animation: fadeIn 0.5s ease-out;
  }
  
  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(-20px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
  
  .title-container {
    text-align: center;
    margin-bottom: 30px;
  }
  
  .title {
    font-size: 28px;
    font-weight: bold;
    color: #28a745;
    text-shadow: 0 0 10px rgba(40, 167, 69, 0.5);
  }
  
  .subtitle {
    font-size: 18px;
    color: #b1b1b1;
  }
  
  .input-group {
    width: 100%;
    margin-bottom: 25px;
    position: relative;
  }
  
  .input-group input {
    width: 94%;
    padding: 20px 15px 10px;
    border: none;
    border-radius: 5px;
    font-size: 16px;
    background-color: rgba(133, 133, 133, 0.1);
    color: #000000;
    transition: all 0.3s ease;
  }
  
  .input-group input:focus {
    outline: none;
    background-color: rgba(255, 255, 255, 0.2);
    box-shadow: 0 0 10px rgba(40, 167, 69, 0.5);
  }
  
  .input-group label {
    position: absolute;
    top: 10px;
    left: 15px;
    font-size: 16px;
    color: #000000;
    pointer-events: none;
    transition: all 0.3s ease;
  }
  
  .input-group input:focus + label,
  .input-group input:valid + label {
    top: 5px;
    left: 15px;
    font-size: 12px;
    color: #28a745;
  }
  
  .l1 {
    width: 100%;
    padding: 15px;
    background-color: #28a745;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 18px;
    transition: background-color 0.3s ease, transform 0.2s ease;
    margin-top: 10px;
  }
  
  .l1:hover {
    background-color: #218838;
    transform: scale(1.02);
  }
  
  .error-message {
    margin-top: 20px;
    color: #ff4d4f;
    font-size: 16px;
    text-align: center;
  }
  
  .login-button {
    margin-top: 20px;
    background-color: transparent;
    color: #007BFF;
    text-decoration: none;
    border: none;
    padding: 0;
    cursor: pointer;
    font-size: 16px;
    transition: color 0.3s ease;
    text-align: center;
    display: block;
  }
  
  .login-button:hover {
    color: #0056b3;
  }
  
  @media (max-width: 576px) {
    .login-box {
      padding: 20px;
    }
  
    .title {
      font-size: 24px;
    }
  
    .subtitle {
      font-size: 16px;
    }
  
    .input-group input {
      padding: 18px 12px 8px;
    }
  
    .l1 {
      padding: 12px;
      font-size: 16px;
    }
  
    .login-button {
      font-size: 14px;
    }
  }
  </style>